package com.hc.conferencecheck.service.impl;

import com.github.pagehelper.PageInfo;
import com.hc.conferencecheck.entity.*;
import com.hc.conferencecheck.mapper.AttendRecordMapper;
import com.hc.conferencecheck.mapper.ConferenceMapper;
import com.hc.conferencecheck.service.AttendRecordService;
import com.hc.conferencecheck.service.ConferenceService;
import com.hc.conferencecheck.utils.PageHelperUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会议对象service实现类
 */
//开启事务管理
@Transactional
@Service
public class ConferenceServiceImpl implements ConferenceService {
    public static final Logger logger = LoggerFactory.getLogger(ConferenceServiceImpl.class);
    @Resource
    private ConferenceMapper conferenceMapper;
    @Resource
    private AttendRecordService attendRecordServiceImpl;
    @Resource
    private AttendRecordMapper attendRecordMapper;

    /**
     * 修改会议对象
     */
    public ResultMsg editConference(Conference conference) {
        int i = conferenceMapper.updateByPrimaryKeySelective(conference);
        if (i == 1) {
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), "修改成功");
        } else {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "修改失败");
        }
    }

    /**
     * 删除会议对象
     */
    public ResultMsg deleteConferenceByPrimaryKey(int conferenceId) {
        int i = conferenceMapper.deleteByPrimaryKey(conferenceId);
        if (i == 1) {
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), "删除成功");
        } else {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "删除失败");
        }
    }

    /**
     * 添加会议对象
     */
    public ResultMsg createConference(Conference conference) {
        int i = conferenceMapper.insert(conference);
        if (i == 1) {
            //返回主键
            Integer id = conference.getId();
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), id);
        } else {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "添加失败");
        }
    }

    /**
     * 申请报名会议接口
     */
    public ResultMsg applyConference(Map<String, Object> params) {
        //获取申请者ID和会议ID
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            logger.error("申请者ID不可为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "申请者ID不可为空");
        }
        int conventionerId = Integer.parseInt(conventionerIdObj.toString());
        //获取要申请的会议的ID
        Object conferenceIdObj = params.get("conferenceId");
        if (conferenceIdObj == null) {
            logger.error("会议ID不可为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "会议ID不可为空");
        }
        int conferenceId = Integer.parseInt(conferenceIdObj.toString());
        //根据用户ID和会议ID查询是否有此申请记录
        AttendRecord attendRecord = attendRecordServiceImpl.selectAttendRecordByDoubleId(conventionerId, conferenceId);
        if (attendRecord != null) {
            //可以查询到该用户对于当前会议的申请记录，需要提示不可重复申请
            logger.error("不可重复申请");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "不可重复申请");
        }
        //获取支付账号；支付金额
        Object payAccountObj = params.get("payAccount");
        if (payAccountObj == null) {
            logger.error("支付账号不可为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "支付账号不可为空");
        }
        String payAccount = payAccountObj.toString();
        Object amountOfPayObj = params.get("amountOfPay");
        if (amountOfPayObj == null) {
            logger.error("支付金额不可为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "支付金额不可为空");
        }
        String amountOfPay = amountOfPayObj.toString();
        Long amountOfPayL = Long.valueOf(amountOfPay);

        //添加会议申请记录
        AttendRecord newAttendRecord = new AttendRecord();
        newAttendRecord.setConventionerId(conventionerId);
        newAttendRecord.setConferenceId(conferenceId);
        newAttendRecord.setApplyTime(new Date());
        //未支付，未签到，申请未通过；
        newAttendRecord.setIsSign((byte) 0);
        newAttendRecord.setIsPay((byte) 0);
        newAttendRecord.setIsApproved((byte) 0);
        newAttendRecord.setPayAccount(payAccount);
        newAttendRecord.setAmountOfPay(amountOfPayL);

        int i = attendRecordMapper.insert(newAttendRecord);
        if (i == 1) {
            //申请成功，发送提示短信；
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), "申请成功");
        } else {
            logger.error("申请失败");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "申请失败");
        }
    }

    /**
     * 根据主键查询会议对象
     */
    public ResultMsg selectConferenceByPrimaryKey(Map<String, Object> params) {
        //获取主键参数
        Object conferenceIdObj = params.get("conferenceId");
        if (conferenceIdObj == null) {
            logger.error("会议ID不可为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "会议ID不可为空");
        }
        String conferenceId = conferenceIdObj.toString();
        int conferenceIdInt = Integer.parseInt(conferenceId);

        Conference conference = conferenceMapper.selectByPrimaryKey(conferenceIdInt);

        if (conference == null) {
            logger.error("查询不到指定会议对象");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "查询不到指定会议对象");
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("dataList", conference);
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), result);
        }
    }

    /**
     * 查询所有的会议对象
     */
    public ResultMsg selectAllConferences(Map<String, Object> params) {
        //处理分页参数
        PageHelperUtils.startPage(params);

        ConferenceExample conferenceExample = new ConferenceExample();
        ConferenceExample.Criteria criteria = conferenceExample.createCriteria();
        criteria.andIdIsNotNull();

        List<Conference> conferences = conferenceMapper.selectByExample(conferenceExample);

        if (conferences != null && conferences.size() > 0) {
            //获取总记录数
            PageInfo<Conference> pageInfo = new PageInfo<>(conferences);
            long pageTotal = pageInfo.getTotal();
            Map<String, Object> result = new HashMap<>();
            result.put("dataList", conferences);
            result.put("pageTotal", pageTotal);
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), result);
        } else {
            logger.error("会议对象为空");
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "会议对象为空");
        }
    }
}
